package com.adou.xml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.adou.domain.City;
import com.adou.domain.Province;
import com.thoughtworks.xstream.XStream;

public class XmlStreamTest2 {
	/**
	 * java对象转xml
	 */
	@Test
	public void test1(){
		Map<Province, List<City>> map = new HashMap<Province, List<City>>();
		// 创建省份
		Province p1 = new Province();
		p1.setId(1);
		p1.setName("黑龙江");

		City c11 = new City();
		c11.setId(1);
		c11.setName("哈尔滨");

		City c12 = new City();
		c12.setId(2);
		c12.setName("大庆");

		List<City> l1 = new ArrayList<City>();
		l1.add(c11);
		l1.add(c12);

		map.put(p1, l1);

		Province p2 = new Province();
		p2.setId(2);
		p2.setName("吉林");

		City c21 = new City();
		c21.setId(1);
		c21.setName("长春");

		City c22 = new City();
		c22.setId(2);
		c22.setName("吉林");

		List<City> l2 = new ArrayList<City>();
		l2.add(c21);
		l2.add(c22);

		map.put(p2, l2);

		Province p3 = new Province();
		p3.setId(3);
		p3.setName("辽宁");

		City c31 = new City();
		c31.setId(1);
		c31.setName("沈阳");

		City c32 = new City();
		c32.setId(2);
		c32.setName("大连");

		List<City> l3 = new ArrayList<City>();
		l3.add(c31);
		l3.add(c32);

		map.put(p3, l3);
		
		XStream xs = new XStream();
		xs.autodetectAnnotations(true);
		
		
		//java 转换 xml
		String xml = xs.toXML(map);
		System.out.println(xml);
		
		
		
		
		

	}
}
